﻿#region

using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using Radovici.SharedLibrary;
using YouTubePlaylist.Models;
using YouTubePlaylist.MusicVideoService;
using YouTubePlaylist.Views;

#endregion

namespace YouTubePlaylist.ViewModels
{
    public class TrackInputViewModel : ViewModel
    {
        #region Private fields

        private readonly SearchModel _acModel = new SearchModel();
        private readonly Track _track;

        #endregion

        public TrackInputViewModel(Track track)
        {
            _track = track;
            _track.PropertyChanged += OnTrackPropertyChanged;
            _acModel.PropertyChanged += OnSearchModelPropertyChanged;
        }

        public bool CanAddTrack
        {
            get { return !string.IsNullOrEmpty(_track.Artist) && !string.IsNullOrEmpty(_track.Name); }
        }

        public Track Track
        {
            get { return _track; }
        }

        public SearchModel AutoComplete
        {
            get { return _acModel; }
        }

        public IEnumerable<string> AutoCompleteTracks
        {
            get
            {
                if (_acModel.Tracks != null)
                {
                    return _acModel.Tracks.Select(t => string.Format("{0} by {1}", t.Name, t.Artist));
                }
                return null;
            }
        }

        private void OnTrackPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Notify("CanAddTrack");
        }

        private void OnSearchModelPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Artists":
                    Notify("Artists");
                    break;
                case "ArtistTracks":
                    Notify("ArtistTracks");
                    break;
            }
        }

        public override UIElement LoadView()
        {
            return new TrackInputCtrl(this);
        }
    }
}